Podemos enumerar los contenidos de un contexto usando list(), listBindings(), o search().
Cuando llamamos a uno de estos m�todos, obtenemos de vuelta una NamingEnumeration.
Cada �tem de la enumeraci�n es un ejemplar de NameClassPair o de una de sus subclases. Para obtener el nombre del �tem, es decir, el nombre del objeto en relaci�n al contexto fuente (el contexto que hemos listado o buscado), usamos NameClassPair.getName().
El string del nombre devuelto por este m�todo es un nombre mixto. Por ejemplo, deber�amos poder volver a alimentar con este nombre los m�todos de Context del contexto fuente.
Sin embargo, algunas veces el servicio o preveedor de servicio subyacente no puede devolver un nombre en relaci�n al contexto fuente, por ejemplo, si el �tem fue recuperado siguiendo una referencia o un alias. Cuando no se puede devolver un nombre relativo, usamos esta string URL para pasarla a los m�todos de InitialContext , como se desribi� en la p�gina anterior.
Para dererminar si el nombre devuelto por getName() es relativo, usamos NameClassPair.isRelative().
Abajo tenemos un ejemplo que busca en un contexto entradas cuyo atributo "cn" empiece con la letra "S." Luego recupera el atributo "telephonenumber" del �tem usando DirContext.getAttributes().
Podr�amos haber hecho esto m�s f�cilmente usando el argumento SearchControls para la petici�n de atributos. Aqu�, el atributo recuperado se ha separado para ilustrar el uso de isRelative().
Cuando el ejemplo obtiene un �tem que contiene un string URL como un nombre (es decir, isRelative() devuelve false), usa InitialContext para procesar el string URL.
// Set up the environment for creating the initial context
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:489/o=JNDItutorial");
// Enable referrals so that we get some nonrelative names
env.put(Context.REFERRAL, "follow");
// Create the initial context
DirContext initCtx = new InitialDirContext(env);
// Get the target context
DirContext targetCtx = (DirContext)initCtx.lookup("ou=All");
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
// Perform the search on the target context
NamingEnumeration enum = targetCtx.search("", "(cn=S*)", constraints);
Attributes attrs;
NameClassPair item;
String[] attrIds = new String[]{"telephonenumber"};
// For each answer found, get its "telephonenumber" attribute
// If relative, resolve it relative to the target context
// If not relative, resolve it relative to the initial context
while (enum.hasMore()) {
item = (NameClassPair)enum.next();
System.out.println(">>>>>" + item.getName() + " ");
if (item.isRelative()) {
attrs = targetCtx.getAttributes(item.getName(), attrIds);
} else {
attrs = initCtx.getAttributes(item.getName(), attrIds);
}
System.out.println(attrs);
}
Aqu� tenemos la salida al ejecutar este programa.
>>>>>ldap://localhost:389/cn=Scott Seligman, ou=People, o=JNDITutorial
{telephonenumber=telephonenumber: +1 408 555 5252}
>>>>>ldap://localhost:389/cn=Samuel Clemens, ou=People, o=JNDITutorial
{telephonenumber=telephonenumber: +1 408 555 0186}
>>>>>ldap://localhost:389/cn=Spuds Mackenzie, ou=People, o=JNDITutorial
{telephonenumber=telephonenumber: +1 408 555 4420}
>>>>>ldap://localhost:389/cn=S. User,ou=NewHires,o=JNDITutorial
No attributes
Tambi�n puedes ir a la lecci�n Referenciadores o a la secci�n Des-referenciar Alias para ver m�s ejemplos y descripciones.